package com.yonyou.crm.sys.dataright.handler;

import java.util.ArrayList;
import java.util.List;

import com.yonyou.crm.sys.dataright.entity.RightObject;
import com.yonyou.crm.sys.dataright.entity.SuperVO;

public class TenantHandler implements RightHandler {

	//权限实体信息
	RightObject rightObj;
	//当前部门
	Long curTenant;
	
	public TenantHandler(RightObject rightObj, Long curTenant) {
		super();
		this.rightObj = rightObj;
		this.curTenant = curTenant;
	}
	
	@Override
	public boolean hasRight(SuperVO vo) {
		return curTenant == getTenantId(vo);
	}
	@Override
	public SuperVO[] filterWithRight(SuperVO[] vos) {
		if(vos == null || vos.length == 0) {
			return vos;
		}
		List<Object> result = new ArrayList<Object>();
		for(int i=0,len=vos.length;i<len;i++) {
			if(curTenant == getTenantId(vos[i])) {
				result.add(vos[i]);
			}
		}
		return result.toArray(new SuperVO[0]);
	}
	@Override
	public String getRightSql() {
		return rightObj.getTenantField() + "=" + curTenant;
	}
	
	private Long getTenantId(SuperVO vo) {
		return vo.getTenantId();
	}
}
	
